package com.example.paichaapp;

import androidx.appcompat.app.AppCompatActivity;

import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.os.Bundle;

public class NfcReadActivity extends AppCompatActivity {
    /**
     *nfc读卡功能
     */
    private NfcAdapter nfcAdapter;
    private PendingIntent pendingIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nfc);
        if (nfcAdapter == null) {
            nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        }
        pendingIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, NfcReadActivity.class), PendingIntent.FLAG_MUTABLE);
        disposeIntent(getIntent());
    }

    @Override
    protected void onStart() {
        super.onStart();
        nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
    }
    @Override
    protected void onPause() {
        super.onPause();
        nfcAdapter.disableForegroundDispatch(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        disposeIntent(intent);
    }

    private void disposeIntent(Intent intent) {
          startActivity(new Intent(NfcReadActivity.this,MainActivity.class));
//        String cardId = getCardId(intent);
//        if (cardId != null) {
//            nfcContentTv.setText(String.format("NFC ID:%s", cardId));
//        } else {
//            Toast.makeText(this, "未读取到卡ID", Toast.LENGTH_SHORT).show();
//        }
    }

}